package com.sskj.ws.factory;

import com.sskj.ws.dto.BasicMessage;
import com.sskj.ws.service.IMessageService;
import org.springframework.stereotype.Component;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

@Component
public class MessageServiceFactory {

    private final Map<String, IMessageService<? extends BasicMessage>> messageServiceMap = new ConcurrentHashMap<>();

    public MessageServiceFactory(List<IMessageService<? extends BasicMessage>> messageServices) {
        if (messageServices != null && !messageServices.isEmpty()) {
            for (IMessageService<?> messageService : messageServices) {
                messageServiceMap.put(messageService.getSupportedType(), messageService);
            }
        }
    }

    @SuppressWarnings("unchecked")
    public <T extends BasicMessage> IMessageService<T> getMessageService(String type) {
        return (IMessageService<T>) messageServiceMap.get(type);
    }
}
